package com.news.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.news.entity.Category;
import com.news.entity.News;
import com.news.service.CategoryService;
import com.news.util.PageModel;

@Controller
@RequestMapping("/system/category")
public class CategoryController {

	// tocategoryindex
	@RequestMapping("/tocategoryindex")
	public String tocategoryindex() {
		return "/tocategoryindex";
	}

	@Resource(name = "categoryManager")
	private CategoryService categoryService;

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	// getallcategory
	@RequestMapping(value = "/getAllCategory")
	@ResponseBody
	public List<News> getAllCategory(HttpServletRequest request) {
		List<News> n = categoryService.getAllCategory();
		return n;
	}

	// id
	@RequestMapping("/getCategory")
	public String getCategory(int cid, HttpServletRequest request,
			HttpSession httpSession) {
		Category c = categoryService.getCategory(cid);
		// request.setAttribute("news",newsService.getNews(newsid));

		httpSession.setAttribute("CID", c.getCid());

		return "categoryindex2";
	}

	// 类别查询
	@RequestMapping("/getAllNewsByCategory")
	@ResponseBody
	public PageModel getAllNewsByCategory(Integer cid,
			HttpServletRequest request, HttpSession httpSession) {
		cid = (Integer) httpSession.getAttribute("CID");

		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		PageModel pageModel = categoryService.getPageModelBycid(page, 4, 2);

		return pageModel;
	}

	@RequestMapping("/getAllNewsByCid")
	@ResponseBody
	public PageModel getAllNewsBypid(Integer cid, HttpServletRequest request,
			HttpSession httpSession) {

		cid = (Integer) httpSession.getAttribute("CID");

		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		PageModel pageModel = categoryService.getPageModelByCid(page, 4, cid);
		// request.setAttribute("pageModel", pageModel);

		return pageModel;
	}

}
